{% extends "base.html" %}
{% block title %}编辑公文{% endblock %}
{% block content %}
<div class="row justify-content-center align-items-center g-1">
  <div class="col-8 pt-5">
    <div class="card">
      <div class="card-header" style="background-color: #f0f0f0; color: #333; font-weight: bold; padding: 10px;">
        编辑公文
      </div>
      <div class="card-body">
        <form method="post" action="{{ url_for('document_edit_commit') }}">
          {{ form.csrf_token }}
          {% if form %}
          <!-- 隐藏字段传递文档ID，这里假设在视图函数中已经将文档ID正确赋值给了form.document_id.data -->
          <input type="hidden" name="document_id" value="{{ form.document_id.data }}">
          <div class="row mb-3">
            {{ form.title.label(class_="col-sm-2 col-form-label", style="text-align: right; padding-top: 7px;") }}
            <div class="col-sm-10">
              {{ form.title(class_="form-control", value=form.title.data, style="border: 1px solid #ccc; border-radius: 4px;") }}
            </div>
          </div>
          <div class="row mb-3">
            {{ form.content.label(class_="col-sm-2 col-form-label", style="text-align: right; padding-top: 7px;") }}
            <div class="col-sm-10">
              {{ form.content(class_="form-control", rows="6", style="border: 1px solid #ccc; border-radius: 4px;") }}
            </div>
          </div>
          <div class="row mb-3">
            {{ form.author_name.label(class_="col-sm-2 col-form-label", style="text-align: right; padding-top: 7px;") }}
            <div class="col-sm-10">
              {{ form.author_name(class_="form-control", readonly=True, style="background-color: #e9e9e9; border: 1px solid #ccc; border-radius: 4px;") }}
            </div>
          </div>
          <div class="row mb-3">
            {{ form.drafting_unit.label(class_="col-sm-2 col-form-label", style="text-align: right; padding-top: 7px;") }}
            <div class="col-sm-10">
              {{ form.drafting_unit(class_="form-control", readonly=True, style="background-color: #e9e9e9; border: 1px solid #ccc; border-radius: 4px;") }}
            </div>
          </div>
          <div class="row mb-3">
            {{ form.receiving_unit.label(class_="col-sm-2 col-form-label", style="text-align: right; padding-top: 7px;") }}
            <div class="col-sm-10">
              {{ form.receiving_unit(class_="form-control", style="border: 1px solid #ccc; border-radius: 4px;") }}
            </div>
          </div>
          <div class="row mb-3">
            {{ form.status.label(class_="col-sm-2 col-form-label", style="text-align: right; padding-top: 7px;") }}
            <div class="col-sm-10">
              {{ form.status(class_="form-control", readonly=True, style="background-color: #e9e9e9; border: 1px solid #ccc; border-radius: 4px;") }}
            </div>
          </div>
          <div class="row mb-3">
            {{ form.security_level.label(class_="col-sm-2 col-form-label", style="text-align: right; padding-top: 7px;") }}
            <div class="col-sm-10">
              {{ form.security_level(class_="form-select w-100", style="max-width: 300px; border: 1px solid #ccc; border-radius: 4px;") }}
            </div>
          </div>
          <div class="row mb-3">
            {{ form.modified_date.label(class_="col-sm-2 col-form-label", style="text-align: right; padding-top: 7px;") }}
            <div class="col-sm-10">
              {{ form.modified_date(class_="form-control", readonly=True, style="background-color: #e9e9e9; border: 1px solid #ccc; border-radius: 4px;") }}
            </div>
          </div>
          <div class="row mb-3 justify-content-center align-items-center">
            <div class="d-flex justify-content-between" style="width: 100%;">
              <a href="/document" class="btn btn-secondary align-buttons" style="width: 24%; padding: 8px 12px; border-radius: 4px; cursor: pointer;">
                取消操作
              </a>
              <button type="submit" name="action" value="save_draft" class="btn btn-secondary" style="width: 24%; padding: 8px 12px; border-radius: 4px; cursor: pointer;">
                保存草稿
              </button>
              <button type="submit" name="action" value="submit_review" class="btn btn-primary" style="width: 24%; padding: 8px 12px; border-radius: 4px; cursor: pointer;">
                提交审核
              </button>
            </div>
          </div>
          {% endif %}
        </form>
      </div>
    </div>
  </div>
</div>
<script>
  document.addEventListener('DOMContentLoaded', function () {
    const submitButtons = document.querySelectorAll('button[type="submit"]');
    submitButtons.forEach(function (button) {
      button.addEventListener('click', function (event) {
        const title = document.getElementById('title').value;
        if (title.length > 255) {
          alert('标题长度不能超过255个字符，请修改后再提交。');
          event.preventDefault();
        }
      });
    });
  });
  </script>
{% endblock %}